import {messages} from "share/common";
/**
 * 操作：确认签收按钮
 * 适用：礼品申领单审核完成后申请人确认签收
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'

import { Button, message } from 'antd'

import requestService from 'containers/request/request.service'


class ConfirmationBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      submitLoading: false,
      applicationList: menuRoute.getRouteItem(this.props.backUrlKey,'key')
    }
  }

  // 确认签收
  goConfirmation = () => {
    let {info} = this.props
    let passData = {
      approvalTxt: "",
      conditionIds: [],
      entities: [{
        entityOID: info.applicationOID,
        entityType: 1001,
        approverOID: info.approvalChain.approverOID,
        countersignApproverOIDs: []
      }]
    }
    this.setState({
      submitLoading: true
    }, () => {
      requestService.appointmentCollectionPass(passData).then(res => {
        this.setState({
          submitLoading: false
        })
        if (res.data.successNum === 1) {
          message.success("确认签收成功")
          if (this.state.applicationList) {
            this.context.router.push(this.state.applicationList.url)
          } else {
            window.history.go(-1);
          }
        } else {
          message.error("确认签收失败")
        }
      }).catch(() => {
        this.setState({
          submitLoading: false
        })
      })
    })
  }

  render() {
    let { submitLoading } = this.state
    return(
      <div className="go-back-btn request-btn">
        <Button type="primary" onClick={this.goConfirmation} loading={submitLoading}>{messages("common.confirmation")/*确认签收*/}</Button>
      </div>
    )
  }
}

ConfirmationBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    language: state.main.language,
    newProfile: state.login.options,
  }
}

export default connect(mapStateToProps)(ConfirmationBtn)